{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1d83c1ed",
   "metadata": {},
   "source": [
    "# Flask 数据可视化详解\n",
    "> 1.<https://pyecharts.org/> -- <https://pyecharts.org/#/zh-cn/web_flask?id=flask-%e6%a8%a1%e6%9d%bf%e6%b8%b2%e6%9f%93>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "39866c0a",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'Environment' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 6\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpyecharts\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mglobals\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m CurrentConfig \u001b[38;5;66;03m# pyecharts：A Python Echarts Plotting Library\u001b[39;00m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mjinja2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m markupsafe \u001b[38;5;66;03m# Jinja2 前端页面展示的框架\u001b[39;00m\n\u001b[1;32m----> 6\u001b[0m CurrentConfig\u001b[38;5;241m.\u001b[39mGLOBAL_ENV \u001b[38;5;241m=\u001b[39m \u001b[43mEnvironment\u001b[49m(loader\u001b[38;5;241m=\u001b[39mFileSystemLoader(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m./templates\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[0;32m      7\u001b[0m \u001b[38;5;66;03m# pyecharts 模板 需要在github下载templates的模板文件\u001b[39;00m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpyecharts\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m options \u001b[38;5;28;01mas\u001b[39;00m opts\n",
      "\u001b[1;31mNameError\u001b[0m: name 'Environment' is not defined"
     ]
    }
   ],
   "source": [
    "from flask import Flask # 用python 后端与前端连接的一个框架\n",
    "from pyecharts.globals import CurrentConfig # pyecharts：A Python Echarts Plotting Library\n",
    "from jinja2.utils import markupsafe # Jinja2 前端页面展示的框架\n",
    "\n",
    "\n",
    "CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader(\"./templates\"))\n",
    "# pyecharts 模板 需要在github下载templates的模板文件\n",
    "\n",
    "from pyecharts import options as opts\n",
    "# options 图表的配置项 graph\n",
    "\n",
    "from pyecharts.charts import Bar\n",
    "# Bar 柱状图 pyecharts 封装好的图形，可参见 gallery 示例\n",
    "\n",
    "from pyecharts.charts import Geo\n",
    "from pyecharts.globals import ChartType, SymbolType\n",
    "\n",
    "app = Flask(__name__, static_folder=\"templates\")\n",
    "\n",
    "\n",
    "# 1. 找到合适的可视化图表，找到gallery示例代码，测试一下示例代码，\n",
    "# 2. 准备数据分析结果的参数，替换示例代码的参数\n",
    "# 3. 创建制作图的函数\n",
    "def bar_base() -> Bar:\n",
    "    c = (\n",
    "        Bar()\n",
    "        .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "        .add_yaxis(\"商家A\", [5, 20, 36, 10, 75, 90])\n",
    "        .add_yaxis(\"商家B\", [15, 25, 16, 55, 48, 8])\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title=\"Bar-基本示例\", subtitle=\"我是副标题\"))\n",
    "    )\n",
    "    return c\n",
    "\n",
    "def geo_lines():\n",
    "    c = (\n",
    "        Geo()\n",
    "        .add_schema(maptype=\"china\")\n",
    "        .add(\n",
    "            \"\",\n",
    "            [(\"广州\", 55), (\"北京\", 66), (\"杭州\", 77), (\"重庆\", 88)],\n",
    "            type_=ChartType.EFFECT_SCATTER,\n",
    "            color=\"white\",\n",
    "        )\n",
    "        .add(\n",
    "            \"geo\",\n",
    "            [(\"广州\", \"上海\"), (\"广州\", \"北京\"), (\"广州\", \"杭州\"), (\"广州\", \"重庆\")],\n",
    "            type_=ChartType.LINES,\n",
    "            effect_opts=opts.EffectOpts(\n",
    "                symbol=SymbolType.ARROW, symbol_size=6, color=\"blue\"\n",
    "            ),\n",
    "            linestyle_opts=opts.LineStyleOpts(curve=0.2),\n",
    "        )\n",
    "        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title=\"Geo-Lines\"))\n",
    "    )\n",
    "    return c\n",
    "\n",
    "# 4.将可视化图形markup方法，绘制在HTML的页面上\n",
    "\n",
    "@app.route(\"/\")\n",
    "def index():\n",
    "    c = geo_lines()\n",
    "    return markupsafe.Markup(c.render_embed())\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be50fea0",
   "metadata": {},
   "source": [
    "# Flask 前后端分离\n",
    "> <https://pyecharts.org/#/zh-cn/web_flask?id=flask-%e5%89%8d%e5%90%8e%e7%ab%af%e5%88%86%e7%a6%bb>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6bd0d369",
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import randrange\n",
    "\n",
    "from flask import Flask, render_template\n",
    "\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Bar\n",
    "\n",
    "\n",
    "app = Flask(__name__, static_folder=\"templates\")\n",
    "\n",
    "\n",
    "def bar_base() -> Bar:\n",
    "    c = (\n",
    "        Bar()\n",
    "        .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "        .add_yaxis(\"商家A\", [randrange(0, 100) for _ in range(6)])\n",
    "        .add_yaxis(\"商家B\", [randrange(0, 100) for _ in range(6)])\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title=\"Bar-基本示例\", subtitle=\"我是副标题\"))\n",
    "    )\n",
    "    return c\n",
    "\n",
    "def geo_lines():\n",
    "    c = (\n",
    "        Geo()\n",
    "            .add_schema(maptype=\"广东\")\n",
    "            .add(\n",
    "                \"geo\",\n",
    "                [list(z) for z in zip(Faker.guangdong_city, Faker.values())],\n",
    "                type_=ChartType.HEATMAP,\n",
    "            )\n",
    "            .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "            .set_global_opts(\n",
    "                visualmap_opts=opts.VisualMapOpts(), title_opts=opts.TitleOpts(title=\"Geo-广东地图\")\n",
    "            )\n",
    "        )\n",
    "\n",
    "\n",
    "@app.route(\"/\")\n",
    "def index():\n",
    "    return render_template(\"index.html\")\n",
    "\n",
    "\n",
    "@app.route(\"/barChart\")\n",
    "def get_bar_chart():\n",
    "    c = bar_base()\n",
    "    return c.dump_options_with_quotes()\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77af2906",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
